package com.cinema.model;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Screening {
    private String screeningId;
    private Movie movie;
    private CinemaHall hall;
    private LocalDateTime startTime;
    private LocalDateTime endTime;
    private double price;
    private int soldTickets;

    public Screening(String screeningId, Movie movie, CinemaHall hall,
                     LocalDateTime startTime, double price) {
        this.screeningId = screeningId;
        this.movie = movie;
        this.hall = hall;
        this.startTime = startTime;
        this.endTime = startTime.plusMinutes(movie.getDuration());
        this.price = price;
        this.soldTickets = 0;
    }

    // Getters
    public String getScreeningId() { return screeningId; }
    public Movie getMovie() { return movie; }
    public CinemaHall getHall() { return hall; }
    public LocalDateTime getStartTime() { return startTime; }
    public LocalDateTime getEndTime() { return endTime; }
    public double getPrice() { return price; }
    public int getSoldTickets() { return soldTickets; }
    public int getAvailableSeats() {
        return hall.getTotalSeats() - soldTickets;
    }

    public void setPrice(double price) { this.price = price; }

    public boolean sellTicket() {
        if (getAvailableSeats() > 0) {
            soldTickets++;
            return true;
        }
        return false;
    }

    public boolean refundTicket() {
        if (soldTickets > 0) {
            soldTickets--;
            return true;
        }
        return false;
    }

    public double calculateRevenue() {
        return soldTickets * price;
    }

    @Override
    public String toString() {
        return String.format("场次ID: %s | 电影: %s | 影厅: %s | 时间: %s | 票价: ¥%.2f | 余票: %d",
                screeningId, movie.getTitle(), hall.getHallName(),
                startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")),
                price, getAvailableSeats());
    }
}